// Copyright 2011, Vanya Davidenko.
// Используемая кодировка: UTF-8.

#ifndef LIBV_FILE_BASIC_REQUEST_HANDLE_H_
#define LIBV_FILE_BASIC_REQUEST_HANDLE_H_

#include <utility>
#include <libv/atomic.h>
#include <libv/stdint.h>
#include <libv/file/afile.h>
#include <libv/file/basic_file.h>



namespace libv
{

typedef ::std::pair<fpos64, uint32_t> FileSlice;



class BasicRequestHandle
{
  public:
    enum Status { CANCELED = -2, FAILED = -1, PENDING = 0, COMPLETED = 1 };
    enum Operation { NONE = 0, READING = 1, WRITING = 2 };

    BasicRequestHandle()
        : file_handle_(LIBV_FILE_INVALID_HANDLE),
          file_slice_(FileSlice(0,0)),
          last_error_(LIBV_FILE_NO_ERROR),
          operation_(NONE) {}


    BasicRequestHandle(FileHandle file_handle,
                       const FileSlice& file_slice,
                       enum Operation operation,
                       const ErrorCode last_error = LIBV_FILE_NO_ERROR)
        : file_handle_(file_handle),
          file_slice_(file_slice),
          last_error_(last_error),
          operation_(operation) {}


    enum Operation operation() const
    {
        return operation_;
    };


    FileSlice file_slice() const
    {
        return file_slice_;
    }


    ErrorCode last_error() const
    {
        return last_error_;
    }


  protected:
    ~BasicRequestHandle() {}

    // Структура с таким порядком полей имеет наименьший размер.
    FileHandle file_handle_;
    FileSlice file_slice_;
    mutable ErrorCode last_error_;
    /* const */ enum Operation operation_;
};

}  // libv

#endif  // LIBV_FILE_BASIC_REQUEST_HANDLE_H_
